<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!-- 工序数据 -->
      <el-col :span="3" :xs="24">
        <el-input
          v-model="taskName"
          placeholder="请输入任务名称或者编号"
          clearable size="small"
          prefix-icon="el-icon-search"
          style="margin-bottom: 20px"/>
        <el-tree   ref="myTree" node-key="id" highlight-current :data="taskList"
                   :default-checked-keys="checkDefault"
                   :props="defaultProps" @node-click="lineClick">
          <template v-slot="{node}">
            <span :title="node.label" class="node-label">{{node.label}}</span>
          </template>
        </el-tree>

      </el-col>
      <el-col :span="21" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工单名称" prop="workOrderName">
        <el-input v-model="queryParams.workOrderName" placeholder="请输入工单名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="工单编号" prop="workOrderNo">
        <el-input v-model="queryParams.workOrderNo" placeholder="请输入工单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="工单状态" prop="workOrderStauts">
        <el-select v-model="queryParams.workOrderStauts" placeholder="请选择工单状态" clearable>
          <el-option v-for="dict in workStauts" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['zndd:workorder:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="workorderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" />
      <el-table-column label="工单名称" align="center" prop="workOrderName"  width="300"  show-overflow-tooltip/>
      <el-table-column label="工单编号" align="center" prop="workOrderNo" width="260" show-overflow-tooltip/>
      <el-table-column label="工序编号" align="center" prop="processNo" width="180" />
      <el-table-column label="工序名称" align="center" prop="processName" width="180"/>
      <el-table-column label="生产总车序" align="center" prop="ji" width="130" />
      <el-table-column label="任务车序" align="center" prop="taskJi" width="130" />
      <el-table-column label="第几道工序" align="center" prop="productionLineSequence" width="130" />
      <el-table-column label="工单状态" align="center" prop="workOrderStauts" :formatter="workStautsFomatter" width="130"/>
      <el-table-column label="预计开始时间" align="center" prop="exStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.exStartTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计完成时间" align="center" prop="exEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.exEndTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上次预计开始时间" align="center" prop="sexStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sexStartTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上次预计结束时间" align="center" prop="sexEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sexEndTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际开始时间" align="center" prop="acStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.acStartTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际结束时间" align="center" prop="acEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.acEndTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['zndd:workorder:edit']">修改</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['zndd:workorder:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
      </el-col>
    </el-row>


    <!-- 修改任务工单信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工单名称" prop="workOrderName">
          <el-input v-model="form.workOrderName" placeholder="请输入工单名称" />
        </el-form-item>
        <el-form-item label="工单编号" prop="workOrderNo">
          <el-input v-model="form.workOrderNo" placeholder="请输入工单编号" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWorkorder, getWorkorder, addWorkorder, updateWorkorder,addsheng} from "@/api/zndd/work/workorder";
import { listTask,listTaskInfo } from "@/api/zndd/task/task";
import { getDicts } from "@/api/system/dict/data";
export default {
  name: "Workorder",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      sheng:true,
      shang:true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      //作业类型列表
      workStauts: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务工单信息表格数据
      workorderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //任务查询数据
      taskName: undefined,
      //当前Id
      currentTaskId: undefined,
/*       taskParams: {
        pageNum: 1,
        pageSize: 50,
        deptId: null,
        taskParam:null
      }, */
      checkDefault: [],//存放默认选中节点的id
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      taskList:[],
      // 查询参数
      queryParams: {
        id: null,
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        mesNo: null,
        taskNo: null,
        taskId: null,
        taskName: '',
        name: '',
        processId: null,
        workOrderName: null,
        workOrderNo: null,
        workOrderStauts: null,
        exStartTime: null,
        exEndTime: null,
        sexStartTime: null,
        sexEndTime: null,
        acStartTime: null,
        acEndTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    //获取字典表中工单状态
     this.getDicts("zndd_worker_order_status").then(response => {
       this.workStauts = response.data;
      });
    this.gettaskList();
  },
  // 查询任务名称
  watch: {
    taskName(newVal) {
      this.queryParams.name = newVal;
      console.log(this.queryParams.name); // 添加日志输出，确保监听器生效
      this.gettaskList();
    }
  },
  methods: {
     //格式化工单状态
    workStautsFomatter(row, column){
      var ws = null;
      console.log(this.workStauts)
      this.workStauts.forEach(workStauts => {
        if (workStauts.dictValue == row.workOrderStauts) {
          ws = workStauts;
        }
      });
      if(ws == null){
        return "";
      }else{
        if(ws.dictValue==1){
          return <el-tag  type="danger">未就绪</el-tag>;
        }else if(ws.dictValue==2){
          return <el-tag>就绪</el-tag>;
        }else if(ws.dictValue==3){
          return <el-tag type="success">进行中</el-tag>;
        }else if(ws.dictValue==4){
          return <el-tag type="info">已完成</el-tag>;
        }else if(ws.dictValue==5){
          return <el-tag type="warning">异常</el-tag>;
        }
        return ws.dictLabel;
      }
    },


     //任务查询
    seachTask(){
       this.taskParams.taskParam=this.taskParam;
       this.gettaskList();
    },

     //树型工序点击事件
    lineClick(data) {
      this.queryParams.taskId = data.value;
/*       this.currentTaskId = data.value;
      this.queryParams.pageNum=1; */
      this.getList();
    },
    /** 查询任务列表 */
    gettaskList(){
      listTaskInfo(this.queryParams.name).then(response => {
        var list = [];
        response.forEach(row => {
          list.push({
            label: row.name,
            value: row.id,
            id: row.id,
          })
        });
        console.log(list);
        this.taskList = list;

        if (list.length > 0) {
          this.queryParams.taskId = list[0].id;
          this.currentTaskId = list[0].id;
        }
        this.checkDefault=[this.currentTaskId];
        this.$nextTick(() => {
          this.$refs.myTree.setCurrentKey(this.currentTaskId);
        })
        console.log( this.checkDefault)
        this.queryParams.pageNum=1;
        this.getList();
      });
    },
    /** 查询任务工单信息列表 */
    getList() {
      this.loading = true;
      listWorkorder(this.queryParams).then(response => {
        this.workorderList = response.rows;
        this.total = response.total;
        if(this.total>0){
           this.shang=false;
        }else{
          this.sheng=false;
        }
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        mesNo: null,
        taskNo: null,
        taskId: null,
        processId: null,
        workOrderName: null,
        workOrderNo: null,
        workOrderStauts: null,
        exStartTime: null,
        exEndTime: null,
        sexStartTime: null,
        sexEndTime: null,
        acStartTime: null,
        acEndTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加任务工单信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWorkorder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改任务工单信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWorkorder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWorkorder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zndd/workorder/export', {
        ...this.queryParams
      }, `workorder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style lang="css" scoped>
/*设置高亮颜色*/
::v-deep
  .el-tree--highlight-current
  .el-tree-node.is-current
  > .el-tree-node__content {
  background-color: #409EFF !important;
  color: #ffffff !important;
}

.span-ellipsis {
  font-size: 15px;
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  display: block;
}

/* 自定义滚动条样式 */
::v-deep ::-webkit-scrollbar {
  width: 12px; /* 水平滚动条的宽度 */
  height: 12px; /* 垂直滚动条的高度 */
  cursor: pointer; /* 鼠标靠近滚动条时变为小手样式 */
}

::v-deep ::-webkit-scrollbar-track {
  background: #f1f1f1; /* 滚动条轨道的颜色 */
  cursor: pointer; /* 鼠标靠近滚动条轨道时变为小手样式 */
}

::v-deep ::-webkit-scrollbar-thumb {
  background: #888; /* 滚动条滑块的颜色 */
  border-radius: 6px; /* 滚动条滑块的圆角 */
  cursor: pointer; /* 鼠标靠近滚动条滑块时变为小手样式 */
}

::v-deep ::-webkit-scrollbar-thumb:hover {
  background: #555; /* 滚动条滑块悬停时的颜色 */
  cursor: pointer; /* 鼠标悬停在滚动条滑块上时仍保持小手样式 */
}
.node-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
